package cn.itsource.system.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.system.domain.DictionaryDetail;
import cn.itsource.system.query.DictionaryDetailQuery;
import cn.itsource.system.service.IDictionaryDetailService;
import cn.itsource.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
@Api(value = "数据字典明细",description = "数据字典明细相关CRUD")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping("/{id}")
    public DictionaryDetail queryById(@ApiParam(name="ID属性",required = true)@PathVariable Long id){
        return dictionaryDetailService.queryById(id);
    }

    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }

    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery detailQuery){
        return dictionaryDetailService.queryPage(detailQuery);
    }

    @DeleteMapping
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if (dictionaryDetail.getId() == null){
                dictionaryDetailService.add(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

}
